//
//  FirstSearchViewController.m
//  Project_B
//
//  Created by 张闯的mac pro on 15/12/1.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "FirstSearchViewController.h"
#import "AFNetworkHandler.h"
#import "FirstSearActiTableViewCell.h"
#import "FirstSearTopicTableViewCell.h"
#import "FirstModel.h"
#import "FirstTopViewViewController.h"

#define GREENCOLOR [UIColor colorWithRed:60.0 / 255 green:149.0 / 255 blue:58.0 / 255 alpha:1]

@interface FirstSearchViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *topicArr;
@property (nonatomic, strong) NSMutableArray *rootArr;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UIView *BGView;

@end

@implementation FirstSearchViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    

    [self createSearchBar];
    [self createView];
    
}

#pragma mark - 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[FirstSearTopicTableViewCell class] forCellReuseIdentifier:@"topic"];
    [self.tableView registerClass:[FirstSearActiTableViewCell class] forCellReuseIdentifier:@"root"];
}

#pragma mark - 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.topicArr.count + 1;
    } else {
        return self.rootArr.count + 1;
    }
}
#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 40;
        } else {
            return 60;
        }
    } else {
        if (indexPath.row == 0) {
            return 40;
        } else {
            return 130;
        }
    }
}
#pragma mark - 单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            cell.textLabel.text = @"话题";
            cell.textLabel.textColor = GREENCOLOR;
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            return cell;
        } else{
            FirstSearTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topic"];
            if (self.topicArr.count == 0) {
                cell.title.text = @"没有相关话题";
            } else {
                cell.searchText = self.searchBar.text;
                cell.topicModel = self.topicArr[indexPath.row - 1];
            }
            
            return cell;
        }
    } else {
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            cell.textLabel.text = @"线路活动";
            cell.textLabel.textColor = GREENCOLOR;
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            return cell;
        } else {
            FirstSearActiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"root"];
            cell.searchText = self.searchBar.text;
            cell.rootModel = self.rootArr[indexPath.row - 1];
            return cell;
        }
    }
}
#pragma mark - 创建SearchBar
- (void)createSearchBar
{
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 375, 40)];
    self.searchBar.placeholder = @"搜索话题 线路活动";
    [[[[self.searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview];
    self.searchBar.delegate = self;
    self.tableView.tableHeaderView.backgroundColor = [UIColor blackColor];
    self.navigationItem.titleView = self.searchBar;

}
- (void)createView
{
    self.BGView = [[UIView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.BGView];
    self.BGView.userInteractionEnabled = NO;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, (667 - 30 - 64) / 3, 375, 30)];
    label.text = @"没有任何搜索";
    label.textColor = [UIColor lightGrayColor];
    label.textAlignment = NSTextAlignmentCenter;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"model"] == NO) {
    }else{
        label.textColor = [UIColor whiteColor];
    }
    [self.BGView addSubview:label];

}
#pragma mark - 点击搜索后请求数据
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v2/search/page/%@", self.searchBar.text];

    [AFNetworkHandler getUrl:str cookie:nil result:^(id result) {
        NSArray *arr = result[@"data"][@"huatilist"];
        NSArray *arr2 = result[@"data"][@"huodonglist"];
        
        NSLog(@"arr:%@ arr2:%@", arr, arr2);
        
        if (arr.count != 0 && arr2.count != 0) {
            self.topicArr = [FirstModel transformWithArray:arr];
            self.rootArr = [FirstModel transformWithArray:arr2];
            [self.tableView reloadData];
            [self creatTableView];
        } else {
            [self.tableView removeFromSuperview];
//            [self createView];
        }

    }];
};
#pragma mark - 点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
        } else {
            FirstTopViewViewController *topic = [[FirstTopViewViewController alloc] init];
            topic.idString = [self.topicArr[indexPath.row - 1] first_id];
            [self.navigationController pushViewController:topic animated:YES];
        }
    } else {
        if (indexPath.row == 0) {
            
        } else {
            FirstTopViewViewController *topic = [[FirstTopViewViewController alloc] init];
            topic.idString = [self.rootArr[indexPath.row - 1] first_id];
            [self.navigationController pushViewController:topic animated:YES];

        }
    }
    [self.searchBar resignFirstResponder];
}
#pragma mark - 回收键盘
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //失去第一响应
    [self.searchBar resignFirstResponder];
}



- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
